/*
 * DbConfigView.java
 *
 * Created on 23 Eylül 2008 Salı, 15:06
 */
package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.DbConfigController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.utilities.Validatable;
import org.testis.gui.mvc.utilities.ValidationUtils;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author  nurhat
 */
public class DbConfigView extends AtofBaseDialog implements
        AbstractViewPanel,
        Validatable {

    private static final long serialVersionUID = 203213L;
    private String m_windowTitle;

    /** Creates new form DbConfigView */
    public DbConfigView(java.awt.Frame parent,
            boolean modal) {
        this(parent, modal, false);
    }

    public DbConfigView(Frame parent,
            boolean modal,
            boolean editMode) {
        super(parent, modal);
        if (editMode) {
            m_windowTitle = "Edit  Configuration";
        } else {
            m_windowTitle = "Add  Configuration";
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtDatabaseUrl = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCatalogName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtActiveMqUrl = new javax.swing.JTextField();
        btnClose = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(m_windowTitle);
        setLocationByPlatform(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setText("Database Url:");

        txtDatabaseUrl.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtDatabaseUrlInputMethodTextChanged(evt);
            }
        });

        jLabel2.setText("Username:");

        txtUsername.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtUsernameInputMethodTextChanged(evt);
            }
        });

        jLabel3.setText("Password:");

        txtPassword.setAlignmentX(100);
        txtPassword.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtPasswordInputMethodTextChanged(evt);
            }
        });

        jLabel4.setText("Catalog Name:");

        txtCatalogName.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtCatalogNameInputMethodTextChanged(evt);
            }
        });

        jLabel5.setText("ActiveMq Url:");

        txtActiveMqUrl.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtActiveMqUrlInputMethodTextChanged(evt);
            }
        });

        btnClose.setMnemonic(KeyEvent.VK_ESCAPE);
        btnClose.setText("Continue");
        getRootPane().setDefaultButton(btnClose);
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtDatabaseUrl, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(32, 32, 32)
                        .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(34, 34, 34)
                        .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtActiveMqUrl, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                            .addComponent(txtCatalogName, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnClose)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnClose, btnSave});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDatabaseUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCatalogName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtActiveMqUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
    this.setVisible(false);
    this.setEnabled(false);
    this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    if (!validateForm()) {
        return;
    }
    TestDesignControllerList.newInstance().getDbConfigController().
            operationSaveChanges(txtDatabaseUrl.getText(),
            txtUsername.getText(),
            txtPassword.getText(),
            txtCatalogName.getText(),
            txtActiveMqUrl.getText());
    JOptionPane.showMessageDialog(null,
            "if the application started before this operation, your changes will be active after restarting application.",
            "Warning...",
            JOptionPane.WARNING_MESSAGE);
    this.dispose();

}//GEN-LAST:event_btnSaveActionPerformed

private void txtDatabaseUrlInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_txtDatabaseUrlInputMethodTextChanged
{//GEN-HEADEREND:event_txtDatabaseUrlInputMethodTextChanged
    TestDesignControllerList.newInstance().getDbConfigController().
            operationSetDataBaseUrl(txtDatabaseUrl.getText());
}//GEN-LAST:event_txtDatabaseUrlInputMethodTextChanged

private void txtUsernameInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_txtUsernameInputMethodTextChanged
{//GEN-HEADEREND:event_txtUsernameInputMethodTextChanged
    TestDesignControllerList.newInstance().getDbConfigController().
            operationSetUsername(txtUsername.getText());
}//GEN-LAST:event_txtUsernameInputMethodTextChanged

private void txtPasswordInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_txtPasswordInputMethodTextChanged
{//GEN-HEADEREND:event_txtPasswordInputMethodTextChanged
    TestDesignControllerList.newInstance().getDbConfigController().
            operationSetPassword(txtPassword.getText());
}//GEN-LAST:event_txtPasswordInputMethodTextChanged

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    WindowListener[] listeners = getWindowListeners();
    for (int i = 0; i < listeners.length; i++) {
        this.removeWindowListener(listeners[i]);
    }
    TestDesignControllerList.newInstance().getDbConfigController().
            operationClose();
}//GEN-LAST:event_formWindowClosed

private void txtCatalogNameInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_txtCatalogNameInputMethodTextChanged
{//GEN-HEADEREND:event_txtCatalogNameInputMethodTextChanged
    TestDesignControllerList.newInstance().getDbConfigController().
            operationSetCatalogName(txtCatalogName.getText());
}//GEN-LAST:event_txtCatalogNameInputMethodTextChanged

private void txtActiveMqUrlInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_txtActiveMqUrlInputMethodTextChanged
{//GEN-HEADEREND:event_txtActiveMqUrlInputMethodTextChanged
    TestDesignControllerList.newInstance().getDbConfigController().
            operationSetActiveMqUrl(txtActiveMqUrl.getText());
}//GEN-LAST:event_txtActiveMqUrlInputMethodTextChanged

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(DbConfigController.CHANGE_DATABASE_URL)) {
            txtDatabaseUrl.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(DbConfigController.CHANGE_USERNAME)) {
            txtUsername.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                DbConfigController.CHANGE_PASSWORD)) {
            txtPassword.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                DbConfigController.CHANGE_CATALOG_NAME)) {
            txtCatalogName.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                DbConfigController.CHANGE_ACTIVEMQ_URL)) {
            txtActiveMqUrl.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                DbConfigController.SHOW_DBCONFIG_VIEW)) {
            this.centerScreen();
        }

    }

    @Override
    public boolean validateForm() {
        boolean valid = true;
        StringBuilder builder = new StringBuilder();
        if (ValidationUtils.isEmpty(txtDatabaseUrl.getText())) {
            builder.append("Database url must not be empty.\n");

            valid = false;
        }
        if (ValidationUtils.isEmpty(txtUsername.getText())) {
            builder.append("Database username must not be empty.\n");

            valid = false;
        }
        if (ValidationUtils.isEmpty(txtCatalogName.getText())) {
            builder.append("Catalog name must not be empty.\n");

            valid = false;
        }
        if (ValidationUtils.isEmpty(txtActiveMqUrl.getText())) {
            builder.append("Activemq url must not be empty.\n");

            valid = false;
        }
        if (!valid) {
            JOptionPane.showMessageDialog(null,
                    builder.toString(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return valid;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtActiveMqUrl;
    private javax.swing.JTextField txtCatalogName;
    private javax.swing.JTextField txtDatabaseUrl;
    private javax.swing.JTextField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
